package org.dyk.mallmanagementsystem.controller;

import org.dyk.mallmanagementsystem.model.Goods;
import org.dyk.mallmanagementsystem.service.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import java.util.List;

@Controller
@RequestMapping(value = {"/goods"})
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 商品管理页面
     * @param model
     * @return
     */
    @RequestMapping(value = {""})
    public String goods(@RequestParam(required = true, defaultValue = "0") int page,
                        @RequestParam(required = true, defaultValue = "10") int size,
                        Model model){
        //设置分页属性
        PageRequest pageRequest = PageRequest.of(page,size);
        Page<Goods> all = goodsService.findAll(pageRequest);
        model.addAttribute("pageAll",all);
        return "goods";
    }

    /**
     * 添加商品 页面
     * @return
     */
    @RequestMapping(value = {"/insertGoods"})
    public String insertGoods(){
        return "insertGoods";
    }

    /**
     * 添加商品 验证
     * @param tid
     * @param goodsName
     * @param goodsPrice
     * @param goodsDescribe
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public boolean insert(@RequestParam int tid,
                          @RequestParam String goodsName,
                          @RequestParam int goodsStock,
                          @RequestParam String goodsPrice,
                          @RequestParam String goodsDescribe,
                          @RequestPart MultipartFile goodsImg){
        boolean insert = goodsService.insert(tid, goodsName, goodsStock, goodsPrice, goodsDescribe, goodsImg);
        if (insert){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改商品 页面
     * @param gid
     * @param model
     * @return
     */
    @RequestMapping(value = {"/updataGoods"})
    public String updataGoods(int gid, Model model){
        //查询需要修改的商品信息
        Goods byGid = goodsService.findByGid(gid);
        model.addAttribute("goods",byGid);
        return "updataGoods";
    }

    /**
     * 修改商品 验证
     * @param gid
     * @param tid
     * @param goodsName
     * @param goodsStock
     * @param goodsPrice
     * @param goodsDescribe
     * @return
     */
    @RequestMapping(value = {"/updata"}, method = RequestMethod.POST)
    @ResponseBody
    public boolean updata(@RequestParam int gid,
                          @RequestParam int tid,
                          @RequestParam String goodsName,
                          @RequestParam int goodsStock,
                          @RequestParam String goodsPrice,
                          @RequestParam String goodsDescribe){
        boolean updata = goodsService.updata(gid, tid, goodsName, goodsStock, goodsPrice, goodsDescribe);
        if (updata){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改图片 验证
     * @param gid
     * @param goodsImg
     * @return
     */
    @RequestMapping(value = {"/updataGoodsImg"}, method = RequestMethod.POST)
    @ResponseBody
    public boolean updataImg(@RequestParam int gid, @RequestPart MultipartFile goodsImg){
        boolean updataGoodsImg = goodsService.updataGoodsImg(gid, goodsImg);
        if (updataGoodsImg){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除商品
     * @param gid
     * @param modelMap
     * @return
     */
    @RequestMapping(value = {"/delete"})
    public String delete(@RequestParam int gid, RedirectAttributesModelMap modelMap){
        boolean delete = goodsService.delete(gid);
        if (delete){
            modelMap.addFlashAttribute("delMsg","删除成功");
            return "redirect:/goods";
        }else {
            modelMap.addFlashAttribute("delMsg","删除失败，该商品仍有未完成订单");
            return "redirect:/goods";
        }
    }

    /**
     * 查询所有商品
     * @return
     */
    @RequestMapping(value = {"/getAllGoods"}, method = RequestMethod.POST)
    @ResponseBody
    public List<Goods> getAllGoods(){
        return goodsService.findAll();
    }

    /**
     * 自定义查询商品
     * @param goodsName
     * @param tid
     * @return
     */
    @RequestMapping(value = {"/selectGoods"}, method = RequestMethod.POST)
    @ResponseBody
    public List<Goods> selectGoods(@RequestParam String goodsName, @RequestParam int tid){
        String goodsLikeName = "%" + goodsName + "%";
        return goodsService.selectByGoods(goodsLikeName,tid);
    }
}
